package lowijs.XfireClient.event;

import java.util.*;
import lowijs.XfireClient.*;

public class EventManager {
	private static final ArrayList<XfireObserver> observers = 
		new ArrayList<XfireObserver>();
	
	private static final ArrayList<XfireEvent> queue = new ArrayList<XfireEvent>();
	
	public static synchronized void addObserver(XfireObserver o) {
		observers.add(o);
		
		for (XfireEvent event : queue) {
			fireEvent(event);
		}
		queue.clear();
	}
	
	public static synchronized void removeObserver(XfireObserver o) {
		observers.remove(o);
	}
	
	public static void cleanup() {
		observers.clear();
		queue.clear();
	}
	
	public static void fireEvent(XfireEvent event) {
		if (observers.size() == 0) {
			queue.add(event);
			return;
		}
		
		for (XfireObserver o : observers) {
			o.handleEvent(event);
		}
	}
}
